<template>
  <div >
    <el-button @click="add">投稿箱子</el-button>
    <el-dialog :visible.sync="dialogVisible" :title="title" width="30%">
      <el-form :model="form" label-width="120px">
        <el-card class="box-card">
          <template v-for="i in tableData">
            <div>
              <span style="display: block">投稿人:{{i.username}}</span>
              <span style="display: block">投稿内容:{{i.contribute}}</span>
              <span style="display: block">投稿时间:{{timeFormat(i.createTime)}}</span>
            </div>
          </template>
        </el-card>
        <br/>
        <span>
          <el-form-item label="" label-width="10px">
          <el-input type="textarea"
                    :rows="2"
                    placeholder="请输入内容"
                    v-model="form.contribute" ></el-input>
        </el-form-item>
        </span>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getQuestionAnswerAdminList ,getQuestionAnswerAdmin} from '@/http/answer.js'
import {addUserContribute, passPay, passUserStatus, queryPay, queryUser, queryUserContribute} from "@/http/user";

export default {
  data() {
    return {
      form: {
        "contribute": "",
      },
      dialogVisible: false,
      currentPage: 1,
      pageSize: 10,
      tableData: [
      ],
      ttid: '',
      total: 0,
      ttcontent: '',
      tpubTime: '',
      title: ''
    }

  },
  created() {
    // this.load()
  },

  methods: {
    timeFormat(time) {
      const date = new Date(time);

      // 获取年、月、日、小时和分钟
      const year = date.getUTCFullYear();
      const month = date.getUTCMonth() + 1; // getUTCMonth() 返回 0-11，所以需要加 1
      const day = date.getUTCDate();
      const hours = date.getUTCHours();
      const minutes = date.getUTCMinutes();
      // 构建格式化的字符串
      return `${year}年${month}月${day}日${hours}点`;
    },
    async load() {
      const { code, message, data } = await queryUserContribute({
        // "current": this.currentPage,
        // "pageSize": this.pageSize,
      })
      console.log(data)
      this.tableData = data
      // this.total = data.total

    },
    add() {
      this.title = '添加投稿'
      this.dialogVisible = true
      this.load()
    },
    // 确认添加
    async save() {
      const { code, message } = await addUserContribute(this.form)
      if (code === 0) {
        this.$message({
          type: "success",
          message: "投稿成功"
        })
      } else {
        this.$message({
          type: "error",
          message: "投稿失败:"+ message
        })
      }
      this.dialogVisible = false
      this.load()
    },
    // 编辑
    handleEdit(row) {
      console.log(row);
      this.title = '编辑'
      this.form = { userId:row.userId ,   "reviewComment": "",
        "status": '', }
      this.dialogVisible = true
    },

    async handleDelete(row) {

      const { code, message } = await getQuestionDelete(row.postId)
      if (code == '0') {
        this.$message({
          type: "success",
          message: "删除成功"
        })
      } else {
        this.$message({
          type: "error",
          message: message
        })
      }
      this.load()  // 删除之后重新加载表格的数据
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()

    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      this.load()
    },

  },
  computed: {
    ...mapGetters({
      userRole: 'user/getUserRole'
    })
  }

}
</script>

<style></style>